<!--
 * @Descripttion:大区城市门店
 * @Author: caijj
 * @Date: 2020-12-03 15:14:54
 * @LastEditors: caijj
 * @LastEditTime: 2021-03-22 17:01:10
-->
<template>
  <BasicLayout>
    <template #wrapper>
      <search-form
        :is-zero-tab="true"
        :report-type="formData.reportType"
        :is-goods="true"
        :exporting="exporting"
        :is-single-date="true"
        :expdrop="true"
        @export="onExportReport"
        @submit="onSearchReport"
      />
      <table-pagination
        ref="table"
        :key="tableKey"
        :show-summary="showSummary"
        :summary-method="getSummaries"
        :loading="loading"
        :columns="columns"
        :data="tableData"
        :total="total"
        :page-size="pageSize"
        :current-page="pageNo"
        border
        @handlePaginationChange="onPaginationChange"
      >
        <template slot="operate" slot-scope="scope">
          <el-link
            type="primary"
            :underline="false"
            @click="onView(scope.item)"
          >查看详情</el-link>
        </template>
      </table-pagination>
    </template>
  </BasicLayout>
</template>
<script>
import SearchForm from '@/components/business/SearchForm';
import TablePagination from '@/components/common/TablePagination';
import { getReportCity, exportCityReport } from '@/api/retailApi';
import { getDetailsTotal } from '@/api/retailApi';
import exportReportMixin from '@/components/mixins/ExportReport';
import tableSummariesMixin from '@/components/mixins/TableSummaries';
import Columns from './columnsConfig.js';
import { exportFile } from '@/utils/tools';

export default {
  name: 'KAliveCityDdition',
  components: {
    SearchForm,
    TablePagination
  },
  mixins: [exportReportMixin, tableSummariesMixin],
  data() {
    return {
      columns: Columns,
      tableData: [],
      loading: false,
      exporting: false,
      total: 0,
      pageNo: 1,
      pageSize: 10,
      formData: {},
      tableKey: 0
    };
  },
  methods: {
    /* 分页查询 */
    onPaginationChange({ current: pageNo = 1, size: pageSize = 10 } = {}) {
      this.onSearchReport({ ...this.formData, pageNo, pageSize });
    },
    /* 导出报表 */
    onExportReport(form) {
      this.exporting = true;
      exportCityReport({
        ...form,
        pageNo: this.pageNo,
        pageSize: this.pageSize
      }).then((res) => {
        const fileName = `城市报表${Date.now()}${
          form.downType === 0 ? '.xlsx' : '.csv'
        }`;
        exportFile(res, fileName);
        this.exporting = false;
      });
    },
    /* 查询报表 */
    onSearchReport(form) {
      this.loading = true;
      this.tableData = [];
      this.formData = {
        pageNo: 1,
        pageSize: 10,
        ...form
      };
      getReportCity(this.formData)
        .then(({ data, current, resultCode, pageSize, total }) => {
          if (resultCode === '0000') {
            this.tableData = data;
            this.total = total;
            this.pageNo = current;
            this.pageSize = pageSize;
            this.loading = false;
          }
        })
        .catch(() => {
          this.loading = false;
        });
      // 来源tableSummariesMixin
      this.getRetailDetailsTotal(this.formData, getDetailsTotal);
    },
    onView({ rowLabel, cityId }) {
      const params = {
        ...this.formData,
        type: 'city',
        name: rowLabel,
        cities: [cityId]
      };
      this.$store.dispatch('retail/clearParentForm');
      const callback = () => this.$router.push({ path: '/retail/city-edition-details' });
      this.$store.dispatch('retail/setParentForm', {
        params,
        callback
      });
    }
  }
};
</script>
